<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="levelName" label="服务商名称">
          <a-input placeholder="请输入等级名称" v-model="model.levelName" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status" label="状态">
          <a-switch v-model="statusFlag" @change="statusClick()" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rate" label="提成比例">
          <a-input-number v-model="model.rate" :min="0" :precision="2" :defaultValue="0" /> %
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount" label="达标业绩">
          <a-input placeholder="请输入达标业绩" v-model="model.amount" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import Area from '@/components/_util/Area'
import moment from 'moment'

export default {
  name: 'XhdjAgencyModal',
  data() {
    return {
      title: '操作',
      visible: false,
      previewVisible: false,
      isAdminFlag: false,
      statusFlag: false,
      royaltyStatusFlag: false,
      model: { shopType: 0 },
      allList: [], // 省份数据
      citys: [],
      cityss: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      validatorRules: {
        levelName: [{ required: true, message: '请填写等级名称', trigger: 'change' }],
      },
      fileList: [
        {
          uid: '-1',
          name: 'image.png',
          status: 'done',
          url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
        },
      ],
      url: {
        add: '/xhdjAgencyLevel/add',
        edit: '/xhdjAgencyLevel/edit',
      },
    }
  },
  created() {
    this.all = new Area().all
    this.allList = this.all.filter((res) => res.index == 1)
  },
  methods: {
    add() {
      //初始化默认值
      this.edit({})
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.statusFlag = this.isAdminAndstatus(this.model.status)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.isAdminFlag = false
      this.statusFlag = false
      this.royaltyStatusFlag = false
      this.$refs.form.clearValidate()
    },
    // 身份上传
    handleCancels() {
      this.previewVisible = false
    },
    async handlePreview(file) {
      if (!file.url && !file.preview) {
        file.preview = await getBase64(file.originFileObj)
      }
      this.previewImage = file.url || file.preview
      this.previewVisible = true
    },
    handleChange({ fileList }) {
      this.fileList = fileList
    },
    handleOk() {
      const that = this
  
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    // 是否代理

    // 状态
    statusClick() {
      if (this.statusFlag) {
        this.model.status = 1
      } else {
        this.model.status = 0
      }
    },
    // 状态
    royaltyStatusClick() {
      if (this.royaltyStatusFlag) {
        this.model.royaltyStatus = 1
      } else {
        this.model.royaltyStatus = 0
      }
    },
    // 编辑代理和状态
    isAdminAndstatus(value) {
      if (value) {
        return true
      } else {
        return false
      }
    },
    // 下拉框格式转换
    formatData(array) {
      var arr = []
      arr = array.forEach((res) => {
        res.value = res.id
        res.label = res.text
      })
      return array
    },
    cityClick(value, text) {
      this.model.area = null
      this.model.city = null
      this.model.cityName = null
      this.model.provinceName = text.data.props.text
      this.citys = this.all.filter((res) => res.pid == this.model.province)
    },
    citysClicks(value, text) {
      this.model.cityName = text.data.props.text
      this.cityss = this.all.filter((res) => res.pid == this.model.city)
      this.$forceUpdate()
    },
  },
}
</script>

<style lang="less" scoped>
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>
